﻿@using Orchard.Comments.Models;
@using Orchard.Comments;
@using Orchard.Security;
@using Orchard.Utility.Extensions;

@{
    var contextExists = TempData["CreateCommentContext.Name"] != null;
    var name = Convert.ToString(TempData["CreateCommentContext.Name"]);
    var commentText = Convert.ToString(TempData["CreateCommentContext.CommentText"]);
    var email = Convert.ToString(TempData["CreateCommentContext.Email"]);
    var siteName = Convert.ToString(TempData["CreateCommentContext.SiteName"]);
}

@if (Model.ContentPart.Comments.Count > 0) {
<div id="comments">
    <h2 class="comment-count">@T.Plural("1 Comment", "{0} Comments", (int)Model.ContentPart.Comments.Count)</h2>
    @{Html.RenderPartial("ListOfComments", (IEnumerable<Orchard.Comments.Models.CommentPart>)Model.ContentPart.Comments);}
</div>
}

@if (Model.ContentPart.CommentsActive == false) {
    if (Model.ContentPart.Comments.Count > 0) {
        <div id="comments">
            <p class="comment-disabled">@T("Comments have been disabled for this content.")</p>
        </div>
    }
}
else if (WorkContext.CurrentUser == null && !AuthorizedFor(Permissions.AddComment)) {
<h2 id="add-comment">@T("Add a Comment")</h2>
<p class="info message">@T("You must {0} to comment.", Html.ActionLink(T("log on").ToString(), "LogOn", new { Controller = "Account", Area = "Orchard.Users", ReturnUrl = string.Format("{0}#addacomment", Context.Request.RawUrl) }))</p>
} else {
using (Html.BeginForm("Create", "Comment", new { area = "Orchard.Comments" }, FormMethod.Post, new { @class = "comment-form" })) { 
    @Html.ValidationSummary() 
    if (WorkContext.CurrentUser == null) {

    <fieldset class="who">
        <legend id="add-comment">@T("Add a Comment")</legend> 
    <ol>
        <li>
            <label for="Name">@T("Name")</label>
            <input id="Name" class="text" name="Name" type="text" value="@(contextExists ? name : String.Empty)" />
        </li>
        <li>
            <label for="Email">@T("Email")</label>
            <input id="Email" class="text" name="Email" type="text" value="@(contextExists ? email : String.Empty)"/>
        </li>
        <li>
            <label for="SiteName">@T("Url")</label>
            <input id="SiteName" class="text" name="SiteName" type="text" value="@(contextExists ? siteName : String.Empty)"/>
        </li>
     </ol>
    </fieldset>
    } else {
        @Html.Hidden("Name", WorkContext.CurrentUser.UserName ?? "")
        @Html.Hidden("Email", WorkContext.CurrentUser.Email ?? "")
    }

    <h2 id="commenter">@if (WorkContext.CurrentUser != null) { @T("Hi, {0}!", Convert.ToString(Html.ItemDisplayText(WorkContext.CurrentUser)))}</h2>
    <fieldset class="what">
    <ol>
        <li>
            <label for="comment-text">@T("Comment")</label>
            <textarea id="comment-text" rows="10" cols="30" name="CommentText">@(contextExists ? commentText : String.Empty)</textarea>
        </li>
        <li>
            <button class="primaryAction" type="submit">@T("Submit Comment")</button>
            @Html.Hidden("CommentedOn", (int)Model.ContentPart.ContentItem.Id) 
            @Html.Hidden("ReturnUrl", Context.Request.ToUrlString()) 
            @Html.AntiForgeryTokenOrchard() 
        </li>
    </ol>
    </fieldset>
    }
} 
